<template>
  <div class="channel-edit">
    <!-- 我的频道区域 -->
    <van-cell :border="false">
      <div slot="title" class="titleText">我的频道</div>
      <van-button
        type="danger"
        plain
        round
        size="mini"
        class="vanBtnEdit"
        @click="editBtn"
        >{{ isMychannelEditStatus ? '完成' : '编辑' }}</van-button
      >
    </van-cell>
    <!-- 宫格 -->
    <van-grid :gutter="10" class="myGrid">
      <van-grid-item
        v-for="(channel, index) in myChannels"
        :key="index"
        class="gridItem"
        @click="onChannelclick(channel, index)"
      >
        <div>
          <span
            slot="text"
            class="text"
            :class="{ active: index === active }"
            >{{ channel.name }}</span
          >
        </div>
        <van-icon
          v-show="isMychannelEditStatus && channel.name !== '推荐'"
          class="iconClaer"
          name="clear"
        ></van-icon>
      </van-grid-item>
    </van-grid>
    <!-- 频道推荐区域 -->
    <van-cell :border="false">
      <div slot="title" class="titleText">频道推荐</div>
    </van-cell>
    <!-- 宫格 -->
    <van-grid :gutter="10" class="otherGrid">
      <van-grid-item
        v-for="(channel, index) in recommendChannels"
        :key="index"
        :text="channel.name"
        class="gridItem"
        icon="plus"
        @click="addChannelBtn(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getChannels,
  addUserChannels,
  deleteUserChannels
} from '@/api/channel.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'

export default {
  name: 'ChannelEdit',
  data() {
    return {
      allChannels: [],
      isMychannelEditStatus: false
    }
  },
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  created() {
    this.getAllChannels()
  },
  computed: {
    recommendChannels() {
      // 所有频道 - 我的频道 = 推荐频道
      return this.allChannels.filter(allItem => {
        return !this.myChannels.find(myItem => {
          return myItem.id === allItem.id
        })
      })
    },
    ...mapState(['user'])
  },
  methods: {
    async getAllChannels() {
      try {
        // 获取所有的频道列表数据
        const { data } = await getChannels()
        this.allChannels = data.data.channels
      } catch (err) {
        this.$toast('获取频道列表数据失败', err)
      }
    },
    // 添加推荐频道按钮
    async addChannelBtn(rChannel) {
      this.myChannels.push(rChannel)
      // 数据持久化处理
      if (this.user) {
        try {
          // 已登录，把数据存储在线上
          await addUserChannels({
            id: rChannel.id, // 频道id
            seq: this.myChannels.length // 序号
          })
        } catch (err) {
          this.$toast('保存失败，请稍后重试')
        }
      } else {
        // 未登录，把数据存储在本地
        setItem('MY_CHANNELS', this.myChannels)
      }
    },
    // 我的频道中的编辑按钮
    editBtn() {
      this.isMychannelEditStatus = !this.isMychannelEditStatus
    },
    // 切换编辑和添加状态
    onChannelclick(channel, index) {
      if (this.isMychannelEditStatus) {
        // 编辑删除状态
        if (channel.name === '推荐') return
        if (index <= this.active) {
          this.$emit('update:active', this.active - 1)
        }
        this.myChannels.splice(index, 1)
        this.deleteChannels(channel)
      } else {
        // 切换状态
        this.$emit('update:active', index)
        this.$emit('close-popup')
      }
    },
    // 删除请求
    async deleteChannels(channel) {
      try {
        if (this.user) {
          // 已登录 将数据更新到线上
          await deleteUserChannels(channel.id)
        } else {
          // 未登录 将数据更新到本地
          setItem('MY_CHANNELS', this.myChannels)
        }
      } catch (err) {
        this.$toast('数据更新失败，请稍后再试', err)
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  .vanBtnEdit {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  .titleText {
    font-size: 32px;
    color: #333;
  }
  /deep/.gridItem {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      background-color: #f4f5f6;
      white-space: nowrap;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
    }
  }
  /deep/.myGrid {
    .gridItem {
      .van-icon-clear {
        position: absolute;
        top: -10px;
        right: -10px;
        font-size: 25px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }
  /deep/.otherGrid {
    .gridItem {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
  .iconClaer {
    position: absolute;
    top: -10px;
    right: -10px;
    color: #cacaca;
    font-size: 36px;
  }
}
</style>
